package com.king.wms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.king.wms.pojo.Buyer;
import com.king.wms.pojo.Storer;
import com.king.wms.service.BuyerService;

@Controller
@RequestMapping(value = "/buyer")
public class BuyerController {
	
	@Autowired
	BuyerService buyerService ;
	
	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Buyer> list_buyer = buyerService.findAll();
		System.out.println("------list_buyer:"+list_buyer);
		request.setAttribute("list_buyer", list_buyer);
		
		request.getRequestDispatcher("/jsp/buyer/buyer.jsp").forward(request,response);
	}
	
	@RequestMapping("/toUpdateBuyerPage/{buyer_id}")
	public void toUpdateStorerPage(@PathVariable Integer buyer_id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		
		System.out.println("=-=-=-=-=------------------------------");
		Buyer buyer = buyerService.findBuyerByBuyer_id(buyer_id);
		System.out.println("===========================buyer"+buyer);
		request.setAttribute("buyer", buyer);
		
		request.getRequestDispatcher("/jsp/buyer/updateBuyer.jsp").forward(
				request, response);
	}
	
	@RequestMapping(value = "/update")
	public String update(Buyer buyer) {
		buyerService.update(buyer);
		return "redirect:/buyer/findAll";
	}
	
	@RequestMapping(value = "/delete/{buyer_id}")
	public String delete(@PathVariable Integer buyer_id) {
		buyerService.delete(buyer_id);
		return "redirect:/buyer/findAll";
	}
	
	@RequestMapping(value = "/insert")
	public String insert(Buyer buyer) {
		buyerService.insert(buyer);
		return "redirect:/buyer/findAll";
	}
}
